package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.LessonClassDO;
import com.bootdo.wx.service.LessonClassService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UUIDUtils;

/**
 * 商品分类
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-06-09 16:06:42
 */
 
@Controller
@RequestMapping("/wx/lessonClass")
public class LessonClassController {
	@Autowired
	private LessonClassService lessonClassService;
	
	@GetMapping()
	@RequiresPermissions("wx:lessonClass:lessonClass")
	String LessonClass(){
	    return "wx/lessonClass/lessonClass";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:lessonClass:lessonClass")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","location");
	    params.put("order","asc");
		//查询列表数据
	    if(params.containsKey("name")&&params.get("name")!=null&&!"".equals(params.get("name").toString())){
	    	 Query query = new Query(params);
	 		List<LessonClassDO> lessonClassList = lessonClassService.likeList(query);
	 		int total = lessonClassList.size();
	 		PageUtils pageUtils = new PageUtils(lessonClassList, total);
	 		return pageUtils;
	    }
        Query query = new Query(params);
		List<LessonClassDO> lessonClassList = lessonClassService.list(query);
		int total = lessonClassService.count(query);
		PageUtils pageUtils = new PageUtils(lessonClassList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:lessonClass:add")
	String add(){
	    return "wx/lessonClass/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:lessonClass:edit")
	String edit(@PathVariable("id") String id,Model model){
		LessonClassDO lessonClass = lessonClassService.get(id);
		model.addAttribute("lessonClass", lessonClass);
	    return "wx/lessonClass/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:lessonClass:add")
	public R save( LessonClassDO lessonClass){
	    lessonClass.setId(UUIDUtils.getUUID());
	    lessonClass.setDelFlag(0);
	    lessonClass.setCaAt(new Date());
		if(lessonClassService.save(lessonClass)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:lessonClass:edit")
	public R update( LessonClassDO lessonClass){
		lessonClass.setUpAt(new Date());
		lessonClassService.update(lessonClass);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:lessonClass:remove")
	public R remove( String id){
		LessonClassDO lessonClass = lessonClassService.get(id);
		lessonClass.setDelFlag(1);
		lessonClass.setUpAt(new Date());
		if(lessonClassService.update(lessonClass)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:lessonClass:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				LessonClassDO lessonClass = lessonClassService.get(string);
				lessonClass.setDelFlag(1);
				lessonClass.setUpAt(new Date());
				lessonClassService.update(lessonClass);
			}
		}
	
		return R.ok();
	}
	
}
